<?
require_once(classdir.'commons.php');
require_once(classdir.'lesson.php');

class lessons extends commons {
	var
		$tbl = 'lessons',
		$tblGroupLessons = 'group_lessons',
		$active = null,
		$parent_list = null;
	function get_items($conditions=array(),$limit=0,$order="`order`",$search_string='') {
		global $db;
		$return = false;
		$this->items = array();
		$where_arr = array();
		foreach($conditions as $key=>$value){
			$key = block($key);
			$value = block($value);
			$where_arr[] = $key." = '$value'";
		}
		if (!empty($where_arr)){
         $where = " WHERE (".implode(") AND (",$where_arr).")";
		}
		$search_string = block(trim($search_string));
		$search_sql =
							empty($search_string)?
							"":
							"AND (
								l.text LIKE '%$search_string%')";

		$this->items = array();
		$sql = "
			SELECT SQL_CALC_FOUND_ROWS
				`id`, `parent_id`, `title`, `text`, `duration`, `order`, `slug`
			FROM
				$this->tbl as l
			$where";

		if (!empty($search_sql)){
			$sql .= $search_sql;
		}

		$sql .= (!empty($order)?" ORDER BY ".block($order):"");

		if (is_array($limit)){
			$sql .= " LIMIT ".(int)$limit[0].",".(int)$limit[1];
		}elseif ($limit!=0){
			$sql .= " LIMIT ".(int)$limit;
		}

		$lessons = $db->query_assoc_count($sql,$count,'id');

		if ($lessons) :
			foreach ($lessons as $id=>$lesson) :
				$this->items[$id] = new lesson($lesson);
			endforeach;			
		endif;
		
		return $count;
	}

	function get_group_lessons($group_id,$limit=0){
		global $db;
		$this->lessons = array();
		$group_id = (int)$group_id;
		$sql = "
			SELECT
				l.id,
				l.parent_id,
				l.title,
				l.text,
				l.duration,
				l.slug
			FROM
				$this->tbl as l
				INNER JOIN
					$this->tblGroupLessons as gl
				ON
					gl.lesson_id = l.id
					AND
					gl.group_id = $group_id
			ORDER BY
				l.order,parent_id
		";
		if (is_array($limit)){
			$sql .= " LIMIT ".(int)$limit[0].",".(int)$limit[1];
		}elseif ($limit!=0){
			$sql .= " LIMIT ".(int)$limit;
		}
		$count = 0;
		$lessons = $db->query_assoc_count($sql,$count);

		if ($lessons){
			foreach ($lessons as $lesson){
				$this->items[] = new lesson($lesson);
			}
		}
		$return = $count;
		return $return;
	}
	
	function build_tree($id=false){
		if (!empty($this->items)){
			$prev = false;
			foreach($this->items as $key=>&$item){
				if ((is_int($id) && $id==$item->id()) || (is_string($id) && $id==$item->slug())){
					$this->set_active($this->items[$key]);
				}
				if (is_object($prev)){
					$item->prev = &$prev;
					$prev->next = &$item;
				}
				$children = new lessons();
				if ($children->get_items(array('parent_id'=>$item->id()))){
					foreach($children->items as $key=>&$child){
						$child->parent = &$item;
					}
					$children->parent_list = &$this;
					$children->build_tree($id);
					$item->children = $children;
				}
				
				$prev = &$item;
			}
		}
	}
	
	function first(){
		$return = false;
		if ($this->count()){
			reset($this->items);
			$return = current($this->items);
		}
		return $return;
	}
	
	function last(){
		$return = false;
		if ($this->count()){
			end($this->items);
			$return = current($this->items);
		}
		return $return;
	}
	
	function set_active(&$active_element){
		$this->active = &$active_element;
		$this->active->active = true;
		if (is_object($this->parent_list)){
			$this->parent_list->set_active($active_element);
		}
	}
}
?>